<template>
  <el-button type="primary" @click="handleClick">
    <slot></slot>
  </el-button>
  <div class="choose-icon-dialog-body-height">
    <el-dialog :title="title" v-model="dialogVisible">
      <div class="container">
        <div
          class="item"
          v-for="(item, index) in Object.keys(Icons)"
          :key="index"
          @click="checkItem(item)"
        >
          <el-icon>
            <component :is="item"></component>
          </el-icon>
          <div class="name">
            {{ item }}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { watch, ref } from 'vue'
import * as Icons from '@element-plus/icons-vue'
import { useCopy } from '@/hooks/use-copy'

const props = defineProps<{
  title: string
  visible: boolean
}>()
const emits = defineEmits(['update:visible'])
// copy visible
const dialogVisible = ref<boolean>(props.visible)
const handleClick = () => {
  emits('update:visible', !props.visible)
}
const checkItem = (item: string) => {
  const text = `<${item} />`
  useCopy(text)
  dialogVisible.value = false
}
watch(
  () => props.visible,
  (val) => {
    dialogVisible.value = val
  }
)
watch(
  () => dialogVisible.value,
  (val) => {
    emits('update:visible', val)
  }
)
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .item {
    width: 20%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-bottom: 15px;
    align-items: center;
    cursor: pointer;
    .el-icon {
      width: 2em;
      height: 2em;
      svg {
        width: 100%;
        height: 100%;
      }
    }
    .name {
      margin-top: 10px;
    }
  }
}
</style>
